package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.CommentMapper;
import cn.tedu.egweibo1.pojo.dto.CommentDTO;
import cn.tedu.egweibo1.pojo.entity.Comment;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/comments")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("insert")
    public int insertComment(@RequestBody CommentDTO commentDTO, HttpSession session){
        System.out.println(commentDTO);
        UserVO userVO= (UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        Comment comment = new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        comment.setWeiboId(commentDTO.getWeiboId());
        commentMapper.insertComment(comment);
        return 1;
    }

}
